/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.model;

import java.util.Date;

/**
 * An item in a project roadmap.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class RoadmapItem {

    private Date due;
    private String name;
    private Long active, closed;

    /**
     * Accessor method.
     *
     * @return a positive integer
     */
    public Long getActive() {
        return sanitize(active);
    }

    /**
     * Accessor method.
     *
     * @param active any (null and negative numbers are set to 0)
     */
    public void setActive(Long active) {
        this.active = sanitize(active);
    }

    /**
     * Accessor method.
     *
     * @return any
     */
    public Date getDue() {
        return due == null ? null : new Date(due.getTime());
    }

    /**
     * Version due date.
     *
     * @param due any
     */
    public void setDue(Date due) {
        this.due = due == null ? null : new Date(due.getTime());
    }

    /**
     * Accessor method.
     *
     * @return any
     */
    public String getName() {
        return name;
    }

    /**
     * Project or current version identifier.
     *
     * @param name any
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Accessor method.
     *
     * @return a positive integer
     */
    public Long getClosed() {
        return sanitize(closed);
    }

    /**
     * Accessor method
     *
     * @param closed any (null and negative numbers are set to 0)
     */
    public void setClosed(Long closed) {
        this.closed = sanitize(closed);
    }

    /**
     * Accessor method.
     *
     * @return a positive > 0 number sum of active and closed
     */
    public Long getTotal() {
        return getActive() + getClosed();
    }

    /**
     * Ensures no value is null or lower than 0.
     *
     * @param value any
     * @return a zero or positive number
     */
    protected Long sanitize(Long value) {
        if ((value != null) && (value.longValue() > 0)) {
            return value;
        }
        return 0L;
    }

    /**
     * Percentage that closed issues represent over the total.
     *
     * @return a positive number
     */
    public long getPercentage() {
        return getTotal() == 0 ? 0 : (100 * getClosed() / getTotal());
    }

}
